using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Konst3d.Efficiency.Suite;

namespace DevUI
{
    public partial class CreateMonitoringGroupDialog : Form
    {
        private IEfficiencySuite _suite;

        public string GroupName
        {
            get
            {
                return uiGroupNameTextBox.Text;
            }
        }

        public string GroupDescription
        {
            get
            {
                return uiGroupDescrTextBox.Text;
            }
        }

        public IEnumerable<int> GetGroupMonitors()
        {
            var monitors = new List<int>();

            foreach (MonitorListItem item in uiGroupAddedMonitorsListBox.Items)
            {
                if (item != null)
                {
                    monitors.Add(item.ID);
                }
            }

            return monitors;
        }

        public IEnumerable<int> GetGroupTasks()
        {
            var tasks = new List<int>();

            foreach (TaskListItem task in uiGroupAddedTasksListBox.Items)
            {
                if (task != null)
                {
                    tasks.Add(task.ID);
                }
            }

            return tasks;
        }

        public CreateMonitoringGroupDialog()
        {
            InitializeComponent();
        }

        private void uiOkButton_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        public DialogResult ShowDialog(IEfficiencySuite suite)
        {
            _suite = suite;

            uiGroupAvailableMonitorsListBox.Items.Clear();
            uiGroupAddedMonitorsListBox.Items.Clear();

            foreach (var monitor in _suite.Monitors)
            {
                uiGroupAvailableMonitorsListBox.Items.Add(new MonitorListItem() { ID = monitor.ID, Name = monitor.Title, Monitor = monitor });
            }

            uiGroupAvailableTasksListBox.Items.Clear();
            uiGroupAddedTasksListBox.Items.Clear();

            foreach (var task in _suite.Tasks)
            {
                uiGroupAvailableTasksListBox.Items.Add(new TaskListItem() { ID = task.ID, Task = task });
            }

            return ShowDialog();
        }

        private void uiGroupAvailableMonitorsListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var selected = uiGroupAvailableMonitorsListBox.SelectedItem;

            if (selected != null)
            {
                uiGroupAddedMonitorsListBox.Items.Add(selected);
                uiGroupAvailableMonitorsListBox.Items.Remove(selected);
            }
        }

        private void uiGroupAddedMonitorsListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var selected = uiGroupAddedMonitorsListBox.SelectedItem;

            if (selected != null)
            {
                uiGroupAvailableMonitorsListBox.Items.Add(selected);
                uiGroupAddedMonitorsListBox.Items.Remove(selected);
            }
        }

        private void uiGroupAvailableTasksListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var selected = uiGroupAvailableTasksListBox.SelectedItem;

            if (selected != null)
            {
                uiGroupAddedTasksListBox.Items.Add(selected);
                uiGroupAvailableTasksListBox.Items.Remove(selected);
            }
        }

        private void uiGroupAddedTasksListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var selected = uiGroupAddedTasksListBox.SelectedItem;

            if (selected != null)
            {
                uiGroupAvailableTasksListBox.Items.Add(selected);
                uiGroupAddedTasksListBox.Items.Remove(selected);
            }
        }
    }
}
